﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Data.Objects;
using System.Data.SqlClient;

namespace Model
{
    public class DataContext
    {

        public static EF4LazyLoadingModel FactoryDataContext()
        {
            String connection = @"Data Source=.\SQLEXPRESS;Initial Catalog=EF4LazyLoadingModel;Integrated Security=SSPI;";
            var builder = new ContextBuilder<EF4LazyLoadingModel>();
            
            //Adicionamos todas as configurações
            builder.Configurations.Add(new PersonConfiguration());
            builder.Configurations.Add(new ContactConfiguration());

            var ctx = builder.Create(new SqlConnection(connection));
            ctx.ContextOptions.LazyLoadingEnabled = true;
            return builder.Create(new SqlConnection(connection));
        }

        /// <summary>
        /// Este método cria o database de acordo com o mapeamento realizado nas classes
        /// de configuração do POCO
        /// </summary>
        public static void CreateDataBase()
        {
            var ctx = FactoryDataContext();
            if (!ctx.DatabaseExists())
                ctx.CreateDatabase();
        }
    }
}
